package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.data.SonHaiBeastBossData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiPlotBattleTypeEnum;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C2_SON_HAI_GET_LOG_LIST, desc = "山海伏兽-获取日志列表")
public class CrossSonHaiGetLogListCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiBattleLogReqMsg reqMsg = SonHaiProto.SonHaiBattleLogReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiBattleLogRespMsg.Builder respMsg = SonHaiProto.SonHaiBattleLogRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_LOG_LIST, respMsg));
            return;
        }
        //获取日志列表
        try {
            //获取日志列表
            List<SonHaiPlotData> plotDataList = new ArrayList<>();
            if (reqMsg.getType() == SonHaiPlotBattleTypeEnum.PLOT_BATTLE_TYPE_NORMAL.getType()) {
                //【探险互助】
                plotDataList = activity.getNormalBattleLogList(packet.getPlayerId());
            } else if (reqMsg.getType() == SonHaiPlotBattleTypeEnum.PLOT_BATTLE_TYPE_BOSS.getType()) {
                //【兽王踪迹】
                List<SonHaiBeastBossData> bossList = activity.getBossBattleLogList(packet.getPlayerId());
                for (SonHaiBeastBossData boss : bossList) {
                    respMsg.addBeastBoss(SonHaiActivityPb.builderSonHaiBeastBossData(boss));
                }
            } else if (reqMsg.getType() == SonHaiPlotBattleTypeEnum.PLOT_BATTLE_TYPE_EGG.getType()) {
                //【兽蛋共享】
                plotDataList = activity.getEggBattleLogList(packet.getPlayerId());
            }
            //获取玩家
            CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
            //构造地块PB
            for (SonHaiPlotData plotData : plotDataList) {
                CrossSonHaiUser pvpUser = null;
                SonHaiEggData eggData = null;
                if (plotData.getPvpUserId() > 0) {
                    pvpUser = activity.getUser(plotData.getPvpUserId());
                }
                if (plotData.getEggId() > 0) {
                    eggData = activity.getEgg(plotData.getEggId());
                }
                SonHaiRegionData regionData = activity.getRegionData(user.getTeamId(), plotData.getRegionId());
                if (regionData != null) {
                    respMsg.addPlotData(SonHaiActivityPb.buildSonHaiPlotData(user, regionData, plotData, null, pvpUser, eggData));
                }
            }
            respMsg.setRet(0);
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_LOG_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }
}
